package com.cake.controller;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.cake.base.util.ReturnUtils;
import com.cake.entity.XxMemberRank;
import com.cake.service.inter.IXxMemberRankService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

/**
 * 会员等级控制层
 * @author Administrator
 *
 */
@RestController
@RequestMapping(value = "memberRank")
public class MemberRankController {

	@Autowired
	private IXxMemberRankService xxMemberRankService;
	
	/**
	 * 查询所有的会员等级记录
	 * @return
	 * @throws Exception 
	 */
	@GetMapping(value = "selectMemberRanks")
	public Object  selectMemberRanks() throws Exception{
		return ReturnUtils.forNoramlSuccess(xxMemberRankService.selectByAttributeAnd(new XxMemberRank()));
	}
	
	/**
	 * 查询所有的会员等级记录
	 * @return
	 * @throws Exception 
	 */
	@PostMapping(value = "selectMemberRanksByPage")
	public Object  selectMemberRanksByPage(@RequestBody XxMemberRank xxMemberRank) throws Exception{
		PageHelper.startPage(xxMemberRank.getCurrentPage(), xxMemberRank.getPageSize());
		List<XxMemberRank> list = xxMemberRankService.selectByAttributeAnd(xxMemberRank);
		PageInfo<XxMemberRank> page = new PageInfo<XxMemberRank>(list);
		return ReturnUtils.forNoramlPage("000000", list, 
				page.getPageNum(), page.getPageSize(), page.getPages(), (int)page.getTotal());
	}
	
	/**
	 * 根据id查询一条会员等级详情
	 * @return
	 * @throws Exception 
	 */
	@GetMapping(value = "selectMemberRankById")
	public Object selectMemberRankById(Long memberRankId) throws Exception{
		return ReturnUtils.forNoramlSuccess(xxMemberRankService.selectByPrimaryKey(memberRankId));
	}
	
	/**
	 * 添加一条会员等级
	 * @param xxMemberRank
	 * @return
	 * @throws Exception
	 */
	@PostMapping(value = "insertOrUpdateMemberRank")
	public Object insertOrUpdateMemberRank(@RequestBody XxMemberRank xxMemberRank) throws Exception{
		XxMemberRank scaleMemberRank = xxMemberRankService.selectMemberRankByGrede(xxMemberRank.getScale());
		XxMemberRank nameMemberRank = xxMemberRankService.selectMemberRankByName(xxMemberRank.getName());
		if(xxMemberRank.getId() == null){
			if(scaleMemberRank != null){
				return ReturnUtils.forNoramlError("500", "111111", "会员等级级数已存在");
			}
			if(nameMemberRank != null){
				return ReturnUtils.forNoramlError("500", "111111", "会员等级级数已存在");
			}
			xxMemberRank.setCreateDate(new Date());
			xxMemberRankService.insertSelective(xxMemberRank);
		}else{
			if((scaleMemberRank != null) && (scaleMemberRank.getId() != xxMemberRank.getId())){
				return ReturnUtils.forNoramlError("500", "111111", "会员等级级数已存在");
			}
			if((nameMemberRank != null) && (nameMemberRank.getId() != xxMemberRank.getId())){
				return ReturnUtils.forNoramlError("500", "111111", "会员等级级数已存在");
			}
			xxMemberRankService.updateByPrimaryKeySelective(xxMemberRank);
		}
		return ReturnUtils.forNoraml("000000","操作成功");
	}	
	
	/**
	 * 修改一条会员等级信息
	 * @param xxMemberRank
	 * @return
	 * @throws Exception 
	 */
	@GetMapping(value = "forbiddenMemberRank")
	public Object updateMemberRan(Long memberRankId) throws Exception{
		XxMemberRank xxMemberRank = xxMemberRankService.selectByPrimaryKey(memberRankId);
		if(xxMemberRank != null){
			xxMemberRank.setIsSpecial(true);
			xxMemberRankService.updateByPrimaryKeySelective(xxMemberRank);
		}
		return ReturnUtils.forNoraml("000000", "操作成功");
	}
}
